package com.luming.ethtran.controller;

import com.luming.ethtran.domain.EthData;
import com.luming.ethtran.domain.Result;
import com.luming.ethtran.mapper.EthDataMapper;
import com.luming.ethtran.service.IEthDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Luming
 * @Date 2024/9/2 15:11
 * @Description:
 */
@RestController
@RequestMapping("/data")
@CrossOrigin(origins = "http://localhost:5173") // 允许跨域的前端地址
public class EthDataController {

    @Autowired
    private IEthDataService iEthDataService;
    
    /**
     * 新增单条数据
     * @param ethData
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody EthData ethData) {
        return iEthDataService.add(ethData);
    }
    
    /**
     * 删除某条数据
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return iEthDataService.delete(id);
    }
    
    /**
     * 更新某条数据
     * @param ethData
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody EthData ethData) {
        return iEthDataService.update(ethData);
    }
    
    /**
     * 根据ID查找订单
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    public Result selectById(@PathVariable Integer id) {
        return iEthDataService.select(id);
    }
    
    /**
     * 分页动态查询
     * @param ethData 请求查询的数据
     * @param pageNum 当前页数
     * @param pageSize 当前页面数据量大小
     * @return
     */
    @GetMapping("/list")
    public Result selectList(
            EthData ethData,
            @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize) {
        return iEthDataService.selectList(ethData, pageNum, pageSize);
    }
    
    /**
     * 折线图
     * 查询sender对应的数据
     * @param sender
     * @return
     */
    @GetMapping("/sender")
    public Result selectSender(@RequestParam String sender) {
        return iEthDataService.selectSender(sender);
    }
    
    /**
     * 折线图
     * 查询receiver对应的数据
     * @param receiver
     * @return
     */
    @GetMapping("/receiver")
    public Result selectReceiver(@RequestParam String receiver) {
        return iEthDataService.selectReceiver(receiver);
    }
    
    /**
     * 饼状图
     * 查询4个sender对应的记录数
     * @param s1
     * @param s2
     * @param s3
     * @param s4
     * @return
     */
    @GetMapping("/countBySenders")
    public Result countBySenders(@RequestParam String s1,
                                 @RequestParam String s2,
                                 @RequestParam String s3,
                                 @RequestParam String s4) {
        return iEthDataService.countBySenders(s1, s2, s3, s4);
    }
    
    /**
     * 饼状图
     * 查询4个receiver对应的记录苏
     * @param r1
     * @param r2
     * @param r3
     * @param r4
     * @return
     */
    @GetMapping("/countByReceiver")
    public Result countByReceiver(@RequestParam String r1,
                                 @RequestParam String r2,
                                 @RequestParam String r3,
                                 @RequestParam String r4) {
        return iEthDataService.countByReceiver(r1, r2, r3, r4);
    }
    
    
    @GetMapping("/cost")
    public Result getCostDistribution() {
        return iEthDataService.getCostDistribution();
    }
}
